package chapter_5.demo4;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author: hero生仔
 */
public class Solution {
    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");
        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        extracted01(transactions);

        extracted02(transactions);

        extracted03(transactions);

        extracted04(transactions);

        extracted05(transactions);

        extracted06(transactions);

        extracted07(transactions);

        extracted08(transactions);

        // 用了rangeClosed方法来生成1到100之间的所有数字。它会产生一个流，然后你可以链接filter方法，只选出偶数
        long count = IntStream.rangeClosed(1, 100).filter(value -> value % 2 == 0).count();
        System.out.println(count);
    }

    private static void extracted08(List<Transaction> transactions) {
        // 找到交易额最小的交易。
        OptionalInt minResult = transactions.stream()
                .mapToInt(Transaction::getValue)
                .reduce(Integer::min);
        if (minResult.isPresent()) {
            System.out.println(minResult.getAsInt());
        } else {
            System.out.println(0);
        }
    }

    private static void extracted07(List<Transaction> transactions) {
        // 所有交易中，最高的交易额是多少？
        OptionalInt maxResult = transactions.stream()
                .mapToInt(Transaction::getValue)
                .reduce(Integer::max);
        if (maxResult.isPresent()) {
            System.out.println(maxResult.getAsInt());
        } else {
            System.out.println(0);
        }
    }

    private static void extracted06(List<Transaction> transactions) {
        // 打印生活在剑桥的交易员的所有交易额。
        Integer sum = transactions.stream()
                .filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity()))
                // 这里的话 map() --> Integer，装箱操作，所以最好是 mapToInt 变成原始的 int 避免装拆箱操作带来的性能消耗
                .mapToInt(Transaction::getValue)
                // 替换了 reduce(0, Integer::sum);
                .sum();
        System.out.println(sum);
    }

    private static void extracted05(List<Transaction> transactions) {
        // 有没有交易员是在米兰工作的？
        List<Trader> result = transactions.stream()
                .filter(transaction -> "Milan".equals(transaction.getTrader().getCity()))
                .map(Transaction::getTrader)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(result);
    }

    private static void extracted04(List<Transaction> transactions) {
        // 返回所有交易员的姓名字符串，按字母顺序排序。
        List<String> result = transactions.stream()
                .sorted(Comparator.comparing(t -> t.getTrader().getName()))
                .map(t -> t.getTrader().getName())
                .distinct()
                .collect(Collectors.toList());
        System.out.println(result);
    }

    private static void extracted03(List<Transaction> transactions) {
        // 查找所有来自于剑桥的交易员，并按姓名排序。
        List<Trader> result = transactions.stream()
                .map(Transaction::getTrader)
                .filter(trader -> "Cambridge".equals(trader.getCity()))
                .sorted(Comparator.comparing(Trader::getName))
                .distinct()
                .collect(Collectors.toList());
        System.out.println(result);
    }

    private static void extracted02(List<Transaction> transactions) {
        // 交易员都在哪些不同的城市工作过？
        List<String> result = transactions.stream()
                .map(t -> t.getTrader().getCity())
                .distinct()
                .collect(Collectors.toList());

        System.out.println(result);
    }

    private static void extracted01(List<Transaction> transactions) {
        // 找出2011年发生的所有交易，并按交易额排序（从低到高）。
        List<Transaction> result = transactions.stream()
                .sorted(Comparator.comparingInt(Transaction::getValue))
                .collect(Collectors.toList());
        System.out.println(result);
    }
}
